# koroFileHeader at Yungoal acer
# Create: 2021-07-06 16:33:53
# LastEdit: 2021-08-13 13:38:07
"""说明暂无"""
__author__ = '749B'

import sys
import textwrap
import unittest

if __name__ == '__main__':
    import os
    sys.path.append(os.getcwd())
# pylint: disable = import-error
from azure_client.models import RuncmdTask


class PrintTitleTestCase(unittest.TestCase):

    def setUp(self):
        self.data = {
            'vm_name': 'host1',
            'resourceGroup': 'rgroup2',
            'OSType': 'LinuxForTest',
            'SubName': 'Sub4',
        }
        self.check_items = ['check1', 'item2', 'any3']
        self.task = RuncmdTask(data=self.data, check_items=self.check_items)

    def test_print_title(self):
        a = ('vm_name,resourceGroup,OSType,SubName', 'check1,item2,any3', 'ERR_code', 'ERR_message')
        b = self.task.print_title()
        self.assertTupleEqual(a, b)


class PrintRowTestCase(unittest.TestCase):

    def setUp(self):
        self.data = {
            'vm_name': 'host1',
            'resourceGroup': 'rgroup2',
            'OSType': 'LinuxForTest',
            'SubName': 'Sub4',
        }
        self.check_items = ['check1', 'item2', 'any3']
        self.task = RuncmdTask(data=self.data, check_items=self.check_items)

    def test_print_row_no_tag(self):
        a = ('host1,rgroup2,LinuxForTest,Sub4', 'yes,no,normal', '', '')
        stdout = textwrap.dedent("""\
        yes
        no
        normal
        """)
        b = self.task.print_row(resp=(stdout, '', ''))
        self.assertTupleEqual(a, b)

    def test_print_row_with_tag(self):
        a = ('host1,rgroup2,LinuxForTest,Sub4', 'yes:no,normal,notag', '', '')
        stdout = textwrap.dedent("""\
        check:yes:no
        item:normal
        notag
        """)
        b = self.task.print_row(resp=(stdout, '', ''))
        self.assertTupleEqual(a, b)

    def test_print_row_rm_tag(self):
        a = ('host1,rgroup2,LinuxForTest,Sub4', 'check:yes:no,item:normal,notag', '', '')
        stdout = textwrap.dedent("""\
        check:yes:no
        item:normal
        notag
        """)
        b = self.task.print_row(rm_tag=False, resp=(stdout, '', ''))
        self.assertTupleEqual(a, b)


class PrintRowMultlineTestCase(unittest.TestCase):
    """加入了多行模式，针对多行模式的测试"""

    def setUp(self):
        self.data = {
            'vm_name': 'test5',
            'resourceGroup': 'steed-dev',
            'OSType': 'LinuxForTest',
            'SubName': 'Yungoal_SI',
        }
        self.check_items = ['username', 'shell']
        self.task = RuncmdTask(data=self.data, check_items=self.check_items)

    def test_print_row_no_error(self):
        a = (
            'test5,steed-dev,LinuxForTest,Yungoal_SI', 
            ['root,/bin/bash', 'azureuser,/bin/bash', 'hermes.dev,/bin/bash', 'zabbix,/sbin/nologin', 'nginx,/sbin/nologin'], 
            '', 
            ''
            )
        stdout = textwrap.dedent("""\
        root,/bin/bash
        azureuser,/bin/bash
        hermes.dev,/bin/bash
        zabbix,/sbin/nologin
        nginx,/sbin/nologin
        """)
        b = self.task.print_row(resp=(stdout, '', ''), multline=True)
        self.assertTupleEqual(a, b)

    def test_print_row_with_error(self):
        a = (
            'test5,steed-dev,LinuxForTest,Yungoal_SI', 
            ',', 
            'AllocationFailed', 
            "'Allocation failed. We do not have sufficient capacity for the requested VM size in this region. Read more about improving likelihood of allocation success at http://aka.ms/allocation-guidance'"
            )
        b = self.task.print_row(resp=(
            '', 
            'AllocationFailed', 
            'Allocation failed. We do not have sufficient capacity for the requested VM size in this region. Read more about improving likelihood of allocation success at http://aka.ms/allocation-guidance'
            ), multline=True)
        self.assertTupleEqual(a, b)


if __name__ == '__main__':
    unittest.main()

